--[[
-- Author:      feilong
-- DateTime:    2020-10-10 17:19:33
-- Description: 节点的服务管理

--]]


local skynet = require "skynet"

local server_config = dofile(skynet.getenv("server_config"))

local table_insert = table.insert
local table_remove = table.remove

local MessageHandler = class("MessageHandler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function MessageHandler:ctor()
	self.services = {}
    self.worker_num = 8
	self:register()	
end

--注册本服务里的消息
function MessageHandler:register()    
    self.message_dispatch = g.MsgDis()
    self.message_dispatch:register('start', handler(self, self.start))
    self.message_dispatch:registerDefault(handler(self, self.defaultMsg))
end

function MessageHandler:showService()  
    --加载了的服务列表
    g.Message():callService(".launcher", "LIST")
    g.Log():debug("list--> ", list, skynet.self())
end

---------------------------------------------------------
-- CMD
---------------------------------------------------------
function MessageHandler:start()
    g.Log():debug("manger start")

    for k = 1, self.worker_num do
        local worker = skynet.newservice("worker/worker_service") --
        g.Message():sendService(worker, "start")
        table_insert(self.services, worker)
    end

end


function MessageHandler:defaultMsg(cmd, uid, ...)
    if not uid then
        g.Log():error("uid is nil")
        return
    end
    local i = uid % #services + 1
    local worker = self.services[i]
    local res = g.Message():callService(worker, cmd, uid, ...)
    return res
end



return MessageHandler